﻿<cms-page-header cms-title="{{vm.user.firstName}} {{vm.user.lastName}}"
                 cms-parent-title="{{vm.userArea.name}} Users"></cms-page-header>

<cms-form cms-name="mainForm"
          cms-edit-mode="vm.editMode"
          ng-submit="vm.save()"
          cms-loading="vm.formLoadState.isLoading">

    <!-- Default toolbar -->
    <cms-page-actions ng-show="!vm.editMode">

        <cms-button cms-text="Edit"
                    ng-if="vm.canUpdate"
                    ng-click="vm.edit()"
                    ng-show="!vm.editMode"
                    ng-disabled="vm.globalLoadState.isLoading"></cms-button>

        <cms-button cms-text="Reset Password"
                    ng-if="::vm.canResetPassword"
                    ng-click="vm.resetPassword()"
                    ng-disabled="vm.editMode || vm.globalLoadState.isLoading"></cms-button>
        
        <cms-button cms-text="Delete"
                    ng-if="::vm.canDelete"
                    ng-click="vm.deleteUser()"
                    ng-disabled="vm.editMode || vm.globalLoadState.isLoading"></cms-button>

    </cms-page-actions>

    <!-- Edit toolbar -->
    <cms-page-actions ng-show="vm.editMode">

        <cms-button-submit cms-text="Save"
                           ng-show="vm.editMode"
                           ng-disabled="vm.mainForm.$invalid || vm.globalLoadState.isLoading"
                           cms-loading="vm.saveLoadState.isLoading"></cms-button-submit>

        <cms-button cms-text="Cancel"
                    ng-click="vm.cancel()"
                    ng-show="vm.editMode"
                    ng-disabled="vm.globalLoadState.isLoading"></cms-button>

    </cms-page-actions>

    <!-- Scrollable content area -->
    <cms-page-body cms-content-type="form">

        <cms-form-status></cms-form-status>

        <!--MAIN-->
        <cms-form-section cms-title="Main">

            <cms-form-field-text cms-title="Display Name"
                cms-model="vm.command.displayName"
                ng-if="vm.userArea.showDisplayName"
                maxlength="150"
                cms-description="A display-friendly name such as a real name or pseudonym."></cms-form-field-text>

            <cms-form-field-text cms-title="Username"
                cms-model="vm.command.username"
                ng-if="::!vm.userArea.useEmailAsUsername"
                ng-required="::!vm.userArea.useEmailAsUsername"
                cms-description="The username that the user uses to sign in with."
                maxlength="150"></cms-form-field-text>

            <cms-form-field-email-address cms-title="Email Address"
                cms-model="vm.command.email"
                maxlength="150"
                ng-required="::vm.userArea.allowPasswordSignIn || vm.userArea.useEmailAsUsername"></cms-form-field-email-address>

            <cms-form-field-dropdown cms-title="Role"
                cms-options="vm.roles"
                cms-option-name="title"
                cms-option-value="roleId"
                cms-model="vm.command.roleId"
                cms-description="The role dictates which permissions a user has."
                cms-readonly="vm.isCurrentUser"
                required></cms-form-field-dropdown>

            <cms-form-field-checkbox cms-title="Require Password Change"
                cms-model="vm.command.requirePasswordChange"
                ng-if="::vm.userArea.allowPasswordSignIn"
                cms-description="If selected, the user will be required to change their password the next time they sign in."></cms-form-field-checkbox>

            <cms-form-field-checkbox cms-title="Account Verified"
                cms-model="vm.command.isAccountVerified"
                ng-if="vm.editMode"
                cms-description="If selected, the user will be marked as a verified account."></cms-form-field-checkbox>

            <cms-form-field-container cms-title="Account Verified"
                cms-description="The date that the user account has been verified."
                ng-if="!vm.editMode">
                <span ng-if="vm.user.accountVerifiedDate">
                    Yes (<cms-time-ago cms-time="vm.user.accountVerifiedDate"></cms-time-ago>)
                </span>
                <span ng-if="!vm.user.accountVerifiedDate">No</span>
            </cms-form-field-container>

            <cms-form-field-checkbox cms-title="Account Active"
                cms-readonly="vm.isCurrentUser"
                cms-model="vm.command.isActive"
                cms-description="If an account is deactivated, the user will be signed out and prevented from signing in.."></cms-form-field-checkbox>

        </cms-form-section>

        <cms-form-section cms-title="Additional Details" ng-if="!vm.isCofoundryAdmin">

            <cms-form-field-text cms-title="First Name"
                cms-model="vm.command.firstName"
                maxlength="32"></cms-form-field-text>

            <cms-form-field-text cms-title="Last Name"
                cms-model="vm.command.lastName"
                maxlength="32"></cms-form-field-text>

        </cms-form-section>

        <!--Logs-->

        <cms-form-section cms-title="Logs">

            <cms-form-field-container cms-title="Last signed in">
                <cms-time-ago cms-time="vm.user.lastSignInDate"></cms-time-ago>
            </cms-form-field-container>

            <cms-form-field-container cms-title="Password last changed"
                                      ng-if="::vm.userArea.allowPasswordSignIn">
                <cms-time-ago cms-time="vm.user.lastPasswordChangeDate"
                              ng-if="vm.user.lastPasswordChangeDate !== vm.user.auditData.createDate"></cms-time-ago>
                <span ng-if="vm.user.lastPasswordChangeDate === vm.user.auditData.createDate">Never</span>
            </cms-form-field-container>

        </cms-form-section>

        <!--AUDIT DATA-->

        <cms-form-section-audit-data cms-audit-data="::vm.user.auditData"></cms-form-section-audit-data>

    </cms-page-body>

</cms-form>